Skip to content

Conversation

@venom1204
Copy link
Contributor

@venom1204 venom1204 commented Jan 20, 2025

Closes #5829.
I have added a condition to validate the right-hand side (RHS) of the := operator. If the RHS is a standalone function, an error is raised with an informative message guiding the user to wrap the function in a list if it is intended to be stored.

@tdhock, could you please review this and let me know for further improvements or additional scenarios that I should do?thank you.

@codecov
Copy link

codecov bot commented Jan 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.69%. Comparing base (2642b84) to head (b295e9b).
Report is 76 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6742   +/-   ##
=======================================
  Coverage   98.69%   98.69%           
=======================================
  Files          79       79           
  Lines       14676    14680    +4     
=======================================
+ Hits        14485    14489    +4     
  Misses        191      191           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@tdhock tdhock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also please add test cases based on my issue

@venom1204

This comment was marked as duplicate.

@venom1204
Copy link
Contributor Author

Hi @tdhock,
I’ve updated the code to add clear and user-friendly error messages in two cases:

  • When a raw function is assigned directly using :=

  • When a single-element list containing a function is assigned without proper recycling

these changes aim to prevent confusing errors and guide users with actionable suggestions.
Please have a look whenever you get the time.
Let me know if I’m missing anything.
Thank you!

test(2318.2, DT[, y := list(mean)], error = "RHS of `:=` is a length-1 list containing a function")
f = mean
test(2318.3, DT[, y := f], error = "RHS of `:=` is a function")
test(2318.4, DT[, y := identity(mean)], error = "RHS of `:=` is a function")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RHS is not a function here

R/data.table.R Outdated
stopf("RHS of `:=` is a function. To create a new column of functions or assign to a non-list-column, it must be wrapped in a list(), e.g., `:= list(myfun)`.")
}
if (is.list(jval) && length(jval) == 1L && is.function(jval[[1L]]) && nrow(x) > 1L) {
stopf("RHS of `:=` is a length-1 list containing a function. `data.table` does not automatically recycle lists. To create a list-column, use `rep(list(myfun), .N)`.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is incorrect.

> data.table(x=1:2, L=list(mean))
       x             L
   <int>        <list>
1:     1 <function[1]>
2:     2 <function[1]>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi
i updated the condition and the tests, can you please review if something is missing/wrong ,when you have time.
thank you.

@venom1204
Copy link
Contributor Author

venom1204 commented Jul 2, 2025

Hi @tdhock, @joshhwuu, @Anirban166,

Progress update for pr

  • Added a check to handle cases where a function (closure) is mistakenly used on the RHS of :=.
  • Specifically checks: is.function(jval)
  • Placed inside the if (length(newnames) > 0) block — so it only triggers when creating a new column.

Prevents malformed data.table objects and confusing errors.

At present, this only covers the function case. Other exotic RHS types (e.g., environments, external pointers) are not yet handled.

Is there anything else that should be checked or added to this condition?

@venom1204 venom1204 requested a review from tdhock July 3, 2025 13:00
@tdhock
Copy link
Member

tdhock commented Jul 11, 2025

please resolve conflicts and click Ready for review.

@venom1204 venom1204 closed this Jul 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

various errors when RHS of := is closure/function

3 participants